#include <iostream>
#include "spotLight.h"


// static members
int spotLight::numLights = 0;

spotLight::spotLight(void)
{
	spotLight::numLights++;
	
	switch(spotLight::numLights)
	{
		case 1:
			lightNumber = GL_LIGHT0;
			break;
		case 2:
			lightNumber = GL_LIGHT1;
			break;
		case 3:
			lightNumber = GL_LIGHT2;
			break;
		case 4:
			lightNumber = GL_LIGHT3;
			break;
		case 5:
			lightNumber = GL_LIGHT4;
			break;
		case 6:
			lightNumber = GL_LIGHT5;
			break;
		case 7:
			lightNumber = GL_LIGHT6;
			break;
		case 8:
			lightNumber = GL_LIGHT7;
			break;
	}

	// default cutoff and attenuation
	spotCutOff = 25;
	quad_attenuation = 0.00001;


	// set light properties
	GLfloat amb[4] = {0.6, 0.6, 0.6, 1.0};
	ambient = new std::vector<GLfloat>(amb, amb + sizeof(amb)/sizeof(GLfloat));

	GLfloat diff[4] = {1.0, 1.0, 1.0, 1.0};
	diffuse = new std::vector<GLfloat>(diff, diff + sizeof(diff)/sizeof(GLfloat));

	GLfloat spec[4] = {1.0, 1.0, 1.0, 1.0};
	specular = new std::vector<GLfloat>(spec, spec + sizeof(spec)/sizeof(GLfloat));

	GLfloat pos[4] = {0, 70, 0.0, 1.0};
	position = new std::vector<GLfloat>(pos, pos + sizeof(pos)/sizeof(GLfloat));

	GLfloat dir[3] = {0, -70, 0};
	direction = new std::vector<GLfloat>(dir, dir + sizeof(dir)/sizeof(GLfloat));

}


void spotLight::setCutOffAngle(GLfloat angle)
{
	spotCutOff = angle;
}

void spotLight::setAttenuation(GLfloat value)
{
	quad_attenuation = value;
}

void spotLight::setPosition(GLfloat x, GLfloat y, GLfloat z)
{
	(*position)[0] = x;
	(*position)[1] = y;
	(*position)[2] = z;
}

void spotLight::setDirection(GLfloat x, GLfloat y, GLfloat z)
{
	(*direction)[0] = x;
	(*direction)[1] = y;
	(*direction)[2] = z;
}

void spotLight::setLightProperty(lightProperties lp, GLfloat red, GLfloat green, GLfloat blue)
{
	std::vector<GLfloat>* temp;

	if(lp == AMBIENT)
	{
		temp = ambient;
	}
	else if(lp == DIFFUSE)
	{
		temp = diffuse;
	}
	else if(lp == SPECULAR)
	{
		temp = specular;
	}

	(*temp)[0] = red;
	(*temp)[1] = green;
	(*temp)[2] = blue;
}

void spotLight::setLightProperty(int lp, GLfloat red, GLfloat green, GLfloat blue)
{
	std::vector<GLfloat>* temp;

	if(lp == 0)
	{
		temp = ambient;
	}
	else if(lp == 1)
	{
		temp = diffuse;
	}
	else if(lp == 2)
	{
		temp = specular;
	}

	(*temp)[0] = red;
	(*temp)[1] = green;
	(*temp)[2] = blue;
}

GLfloat spotLight::getXPos()
{
	return (*position)[0];
}

GLfloat spotLight::getYPos()
{
	return (*position)[1];
}

GLfloat spotLight::getZPos()
{
	return (*position)[2];
}

GLfloat spotLight::getXDir()
{
	return (*direction)[0];
}

GLfloat spotLight::getYDir()
{
	return (*direction)[1];
}

GLfloat spotLight::getZDir()
{
	return (*direction)[2];
}

void spotLight::turnOn()
{
	glLightfv(lightNumber, GL_AMBIENT, ambient->begin()._Ptr);
	glLightfv(lightNumber, GL_DIFFUSE, diffuse->begin()._Ptr);
	glLightfv(lightNumber, GL_SPECULAR, specular->begin()._Ptr);

	glLightfv(lightNumber, GL_POSITION, position->begin()._Ptr);
	glLightfv(lightNumber, GL_SPOT_DIRECTION , direction->begin()._Ptr);

	glLightf(lightNumber, GL_QUADRATIC_ATTENUATION, quad_attenuation);
	glLightf(lightNumber, GL_SPOT_CUTOFF, spotCutOff);
	glLightf(lightNumber, GL_SPOT_EXPONENT, 2.0);
	
	glEnable(lightNumber);
}

void spotLight::turnOff()
{
	glDisable(lightNumber);
}

spotLight::~spotLight(void)
{
	delete ambient;
	delete diffuse;
	delete specular;
	delete position;
	delete direction;

	spotLight::numLights--;
}
